classSolution { public: intcountPairs(vector<int>& nums, int k){ int ans = 0; int n = nums.size(); for (int i = 0; i < n; i ++ ) for (int j = i + 1; j < n; j ++ ) { if (nums[i] == nums[j] and (i * j) % k == 0) ans ++ ; } return ans; } };
classSolution { public: vector<longlong> maximumEvenSplit(longlong s){ using LL = longlong; if (s % 2or s < 0) return {}; if (s == 0) return {0}; vector<LL> ans; for (LL i = 2; ; i += 2) { if (s - i > i) { ans.push_back(i); s -= i; } else { ans.push_back(s); break; } } return ans; } };
voidadd(vector<int>& nums, int x, int c){ int n = nums.size(); for (int i = x; i < n; i += lowbit(i)) nums[i] += c; }
intquery(vector<int>& nums, int x){ int n = nums.size(); int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += nums[i]; return ans; }
classSolution { public: longlonggoodTriplets(vector<int>& nums1, vector<int>& nums2){ using LL = longlong; int n = nums1.size(); map<int, int> f; for (int i = 0; i < n; i ++ ) f[nums1[i]] = i; for (auto& c : nums2) c = f[c] + 1; LL ans = 0; vector<int> L(n + 1, 0), R(n + 1, 0); for (int i = 1; i <= n; i ++ ) add(R, i, 1); for (int i = 0; i < n; i ++ ) { int cur = nums2[i]; // remove from right add(R, cur, -1); int left = query(L, cur - 1), right = query(R, n) - query(R, cur); ans += 1ll * left * right; // add to left add(L, cur, 1); } return ans; } };